﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HPKaraoke
{
    public class PlayListController
    {
        protected List<SongGUI> Songs { get; set; }

        /// <summary>
        /// Chỉ số của bài hát đang phát
        /// </summary>
        public int Indicator { get; set; }

        /// <summary>
        /// Tự động chuyển bài và play
        /// </summary>
        public bool AutoPlay { get; set; }

        public int NumberSongRemain
        {
            get
            {
                if (IsEmpty)
                    return 0;
                return Songs.Count - Indicator;
            }
        }

        public bool IsEmpty
        {
            get
            {
                return (Songs.Count == 0);
            }
        }

        public PlayListController()
        {
            Indicator = -1;
            AutoPlay = false;
            Songs = new List<SongGUI>();
        }

        public int Add(SongGUI s)
        {
            if (Indicator < 0)
                Indicator = 0;
            Songs.Add(s);
            return Songs.Count;
        }

        public void PlayCurrentSong()
        {
            if ((Indicator >= 0) && (Indicator < Songs.Count))
            {
                if (!Songs[Indicator].IsPlaying)
                    Songs[Indicator].Play();
            }
        }

        /// <summary>
        /// Chuyển qua bài hát tiếp theo nếu còn. Ngược lại, trả về false
        /// </summary>
        /// <returns></returns>
        public bool NextSong()
        {
            if ((Indicator >= 0) && (Indicator < Songs.Count) && (Songs[Indicator] != null))
            {
                Songs[Indicator].Stop();
                Songs[Indicator].Dispose();
            }
            Indicator++;
            if (Indicator >= Songs.Count)
            {
                Indicator = -1;
                Songs.Clear();
                return false;
            }
            else
            {
                if (AutoPlay)
                    PlayCurrentSong();
            }
            return true;
        }

        public bool PreviousSong()
        {
            if ((Indicator >= 0) && (Indicator < Songs.Count) && (Songs[Indicator] != null))
            {
                Songs[Indicator].Stop();
                Songs[Indicator].Dispose();
            }
            Indicator--;
            if (Indicator < 0)
            {
                Songs.Clear();
                return false;
            }
            else
            {
                if (AutoPlay)
                    PlayCurrentSong();
            }
            return true;
        }

        public SongGUI GetCurrentSong()
        {
            if (Indicator >= 0)
                return Songs[Indicator];
            else
                return null;
        }

        public void Stop()
        {
            if ((Indicator >= 0) && (Indicator < Songs.Count))
            {
                Songs[Indicator].Stop();
            }
        }

        public void PauseOrResum()
        {
            if ((Indicator >= 0) && (Indicator < Songs.Count))
            {
                Songs[Indicator].PauseOrResume();
            }
        }



        internal string SelectStringOfListAvaiSong()
        {
            string s="";
            if (Indicator < 0)
                return "";
            for (int i = Indicator + 1; i < Songs.Count; ++i)
            {
                s = string.Format("{0}  {1}", s, Songs[i].song.Information.ID);
            }

            return s;
        }
    }
}
